//
//  SEGUsersAuthorViewModel.m
//  SEGViewModel
//
//  Created by 山刘 on 2020/11/23.
//

#import "SEGUsersAuthorViewModel.h"

@interface SEGUsersAuthorViewModel()
@property (nonatomic, strong, readwrite) RACCommand *requestCommand;
@property (strong, nonatomic, readwrite) RACSubject *messageSubject;

@property (strong, nonatomic, readwrite) RACCommand *uploadFileCommand;
@end

@implementation SEGUsersAuthorViewModel
#pragma mark - Init
-(instancetype)init{
    if (self = [super init]) {
        [self racInit];
    }
    return self;
}

-(void)dealloc{
    [self.requestCommand rac_deallocDisposable];
    [self.messageSubject rac_deallocDisposable];
    
    [self.uploadFileCommand rac_deallocDisposable];
}

- (void)racInit {
    
    @weakify(self);
    _requestCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            @strongify(self);
            
            // Code here...
            if (self.enterpriseId.length <=0) {
                [self showMessage:@"请填写企业信息！"];
                [subscriber sendCompleted];
                return nil;
            }
            
            if (self.custName.length <=0) {
                [self showMessage:@"请填写姓名！"];
                [subscriber sendCompleted];
                return nil;
            }
            
            if (self.custName.length > 10) {
                [self showMessage:@"姓名格式不正确!"];
                [subscriber sendCompleted];
                return nil;
            }
            
            [self isShowSVP:YES];
            
            [[SEGUserauthManager sharedManager] requestUserauthWithCommunityid:kAccountCommunityID
                                                                  enterpriseId:self.enterpriseId
                                                                      userType:self.userType
                                                                       userTel:kAccountMobile
                                                                      custName:self.custName
                                                                     imagePath:self.imagePath
                                                                      finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
                
                [self isShowSVP:NO];
                
                if (!isSuccessed) {
                    [self showMessage:errorMessage];
                    [subscriber sendError:nil];
                    return;
                }
                
                [self showMessage:@"提交成功!"];
                [subscriber sendNext:nil];
                [subscriber sendCompleted];
                return;
            }];
            
            return [RACDisposable disposableWithBlock:^{
            }];
        }];
    }];
    
    _uploadFileCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(NSData *  _Nullable input) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            
            // Code here...
            [self isShowSVP:YES];
            [[SEGOnefileuploadManager sharedManager] requestOnefileuploadWithData:input
                                                                         progress:nil
                                                                        completed:^(BOOL isSuccessed, id result, NSString *codeType, NSError *error) {
            
                [self isShowSVP:NO];
                
                if (isSuccessed) {
                    [subscriber sendNext:[SEGOnefileuploadManager sharedManager].oneFileUpload.data];
                    [subscriber sendCompleted];
                    return;
                }
                
                [subscriber sendError:nil];
                [subscriber sendCompleted];
                [self showMessage:error.localizedDescription];
                return;
            }];
            
            return [RACDisposable disposableWithBlock:^{
            }];
        }];
    }];
}

#pragma mark - Publish Methods

#pragma mark - Private Methods
-(void)isShowSVP:(BOOL)show {
    [self.messageSubject sendNext:@{@"code": show ? @"1" : @"2"}];
}
-(void)showMessage:(NSString*)msg {
    [self.messageSubject sendNext:@{@"code":@"3",@"msg":msg}];
}

#pragma mark - Getter
-(RACSubject *)messageSubject{
    if (!_messageSubject) {
        _messageSubject = [RACSubject subject];
    }
    return _messageSubject;
}
#pragma mark - Stter
@end
